//
//  STJsonViewController.m
//  STNewTools
//
//  Created by stoneobs on 17/5/2.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "STJsonViewController.h"
#import "UIViewController+STNavigationTools.h"
@interface STJsonViewController ()
@property(nonatomic,strong)NSArray          *provinceAry;
@property(nonatomic,strong)NSMutableArray   *cityAry;
@property(nonatomic,strong)NSMutableArray   *areaAry;

@property(nonatomic,strong)NSMutableArray            *customCityArray;
@property(nonatomic,strong)NSMutableArray            *customProvinceArray;

@property(nonatomic,strong)NSArray            *myCitysArray;
@end

@implementation STJsonViewController

- (UIImageView *)testImageView{
    if (_testImageView) {
        return _testImageView;
    }
    _testImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
    _testImageView.image = [UIImage imageNamed:@"6"];
    return _testImageView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.view addSubview:self.testImageView];
    [self setRightItemWithTitle:@"write" titleColor:[UIColor orangeColor]];
    NSString * profileUrl = [[NSBundle mainBundle] pathForResource:@"STTools.bundle/STFiles/province1" ofType:@"json"];
    NSData  *prodata = [NSData dataWithContentsOfFile:profileUrl];
    _provinceAry  = [NSJSONSerialization JSONObjectWithData:prodata options:NSJSONReadingAllowFragments error:nil];
    
    NSString * cityfileUrl = [[NSBundle mainBundle] pathForResource:@"STTools.bundle/STFiles/city1" ofType:@"json"];
    NSData  *citydata = [NSData dataWithContentsOfFile:cityfileUrl];
    _cityAry  = [NSJSONSerialization JSONObjectWithData:citydata options:NSJSONReadingAllowFragments error:nil];
    
    NSString * areafileUrl = [[NSBundle mainBundle] pathForResource:@"STTools.bundle/STFiles/area1" ofType:@"json"];
    NSData  *areadata = [NSData dataWithContentsOfFile:areafileUrl];
    _areaAry  = [NSJSONSerialization JSONObjectWithData:areadata options:NSJSONReadingAllowFragments error:nil];
    
    NSString * mycityUrl = [[NSBundle mainBundle] pathForResource:@"STTools.bundle/STFiles/Documentscitys" ofType:@"plist"];
    self.myCitysArray = [NSArray arrayWithContentsOfFile:mycityUrl];
    
    [self whiteToProVience];
  
}

- (void)writecityAreaArray{
    NSLog(@"正在遍历");
    

    NSMutableArray * customCityarray = [NSMutableArray new];
    for (NSDictionary * cityDic in self.cityAry) {
        
        NSDictionary * customDic;
        NSString * cityCode  = [cityDic valueForKey:@"code"];
        NSString * cityParentCode  = [cityDic valueForKey:@"parent_code"];
        NSString * cityName =  [cityDic valueForKey:@"name"];
        NSMutableArray * areaCustomArray = [NSMutableArray new];
        for (NSDictionary * areaDic in self.areaAry) {
            
            NSString * areaParentCode = [areaDic valueForKey:@"parent_code"];
            if ([cityCode isEqualToString:areaParentCode]) {
                [areaCustomArray addObject:areaDic];
                
            }
           
            
        }
        customDic = @{@"code":cityCode,@"parent_code":cityParentCode,@"name":cityName,@"areas":areaCustomArray};
        [customCityarray addObject:customDic];
        
    }

    self.customCityArray = [customCityarray mutableCopy];
    NSLog(@"便利结束");
}
- (void)whiteToProVience{
    NSLog(@"正在遍历");
    
    
    NSMutableArray * customProvinceArray = [NSMutableArray new];
    for (NSDictionary * proDic in self.provinceAry) {
        
        NSDictionary * customProDic;
        NSString * proCode  = [proDic valueForKey:@"code"];
        NSString * proName =  [proDic valueForKey:@"name"];
        NSMutableArray * cityCustomArray = [NSMutableArray new];
        for (NSDictionary * cityDic in self.myCitysArray) {
            
            NSString * cityParentCode = [cityDic valueForKey:@"parent_code"];
            if ([proCode isEqualToString:cityParentCode]) {
                [cityCustomArray addObject:cityDic];
                
            }
            
            
        }
        customProDic = @{@"code":proCode,@"name":proName,@"citys":cityCustomArray};
        [customProvinceArray addObject:customProDic];
        
    }
    
    self.customProvinceArray = [customProvinceArray mutableCopy];
    NSLog(@"便利结束");
    
}
- (void)rightBarAction:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
  //  [self.customCityArray writeToFile:[NSString stringWithFormat:@"%@citys.plist",documentsDirectory] atomically:YES]; //city 重构
     [self.customProvinceArray writeToFile:[NSString stringWithFormat:@"%@provinces.plist",documentsDirectory] atomically:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
